package com.cn.order.util;

import redis.clients.jedis.Jedis;

/**
 * @Description: ChargebackLock
 * @Author: ZhangMao
 * @Date: 2022-03-18
 */


public  class ChargebackLock {
    static Jedis jedis = new Jedis("127.0.0.1",6379);

    /**
     * 退单，锁单
     * @param adminId
     * @param orderId
     */
    public static boolean lock(int adminId,int orderId){
        String keyAdmin = "admin"+adminId;
        String keyOrder = "orderId"+orderId;
        if (jedis.setnx(keyAdmin, String.valueOf(adminId)) == 1L
                &
            jedis.setnx(keyOrder,String.valueOf(orderId)) == 1L) {

            return true;
        }
        else {
            return false;
        }
    }

    /**
     * 退单，解锁
     * @param adminId
     * @param orderId
     */
    public  static  boolean  unLock(int adminId,int orderId){
        String keyAdmin = "admin"+adminId;
        String keyOrder = "orderId"+orderId;
        Long a = jedis.del(keyAdmin,keyOrder);
        if (a == 2){
            return true;
        }else {
            return  false;
        }

    }


}
